package com.kevin.spider.file.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 张帆
 * @date: 2025/4/15 16:06
 */
@ConditionalOnClass({AmazonS3.class})
@ConditionalOnProperty(
        name = {"spring.ai.model.audio.speech"},
        havingValue = "openai",
        matchIfMissing = true
)
@EnableConfigurationProperties({OssProperties.class})
@Configuration
public class AmazonS3AutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public AmazonS3 amazonS3(OssProperties ossProperties) {
        return AmazonS3Client.builder()
                .withClientConfiguration(ossProperties.getClientConfiguration())
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(
                        ossProperties.getEndpoint(), ossProperties.getRegion()))
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ossProperties.getAccessKey(),
                        ossProperties.getSecretKey())))
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();


    }

}
